<!--
 * @Descripttion: unTitle
 * @Author: yizheng.yuan
 * @Date: 2021-10-28 20:34:29
 * @LastEditors: yizheng.yuan
 * @LastEditTime: 2021-10-28 22:52:17
-->
<template>
  <div class="wrapper">
    <div class="pd10">
      <!-- <p class="row_h40">点选地图获取位置</p> -->
      <div class="row_lr row_h40">
        <span>
          点选地图获取位置
        </span>
        <van-button type="info" @click="sure">确定</van-button>
      </div>
      <p class="row_h40">地址：{{obj.address}}</p>
    </div>
    <div style="border:1px solid #ddd;" class="mapBox">
      
      <div id='allmap' style='width: 100%; height: 100%;'></div>

    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      obj:{
        lng: '',
        lat: '',
        address: ''
      }
    }
  },
  mounted(){
    this.init()
  },
  methods:{
    sure(){
      this.$store.commit('updateAddress',this.obj)
      this.$router.push({
        name: 'home',
        // params: this.obj
      })

    },
    init(){

      let that = this;
      //判断是否是空
    function isNull(a) {
      return (a == '' || typeof (a) == 'undefined' || a == null) ? true : false;
    }


    var map = new BMap.Map("allmap");
    var geoc = new BMap.Geocoder(); //地址解析对象
    var markersArray = [];
    var geolocation = new BMap.Geolocation();


    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12); // 中心点
    geolocation.getCurrentPosition(function (r) {
      if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        var mk = new BMap.Marker(r.point);
        map.addOverlay(mk);
        map.panTo(r.point);
        map.enableScrollWheelZoom(true);
      }
      else {
        alert('failed' + this.getStatus());
      }
    }, { enableHighAccuracy: true })
    map.addEventListener("click", showInfo);


    //清除标识
    function clearOverlays() {
      console.log('markersArray',markersArray);
      if (markersArray) {
        for (key in markersArray) {
          map.removeOverlay(markersArray[key])
        }
      }
    }
    //地图上标注
    function addMarker(point) {
      var marker = new BMap.Marker(point);
      markersArray.push(marker);
      clearOverlays();
      map.addOverlay(marker);
    }
    //点击地图时间处理
    function showInfo(e) {
      // document.getElementById('lng').value = e.point.lng;
      // document.getElementById('lat').value = e.point.lat;
      that.obj.lng = e.point.lng;
      that.obj.lat = e.point.lat;

      geoc.getLocation(e.point, (rs)=> {
        var addComp = rs.addressComponents;
        var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
        // document.getElementById('sever_add').value = address;
        that.obj.address = address
        // if (confirm("您选择的地址是" + address + "?")) {
        //
        // }
      });
      addMarker(e.point);
    }
    }
  }
}
</script>

<style scoped>

.wrapper{
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  padding: 30px 0;
}
.mapBox{
  flex: 1;
  overflow: auto;
}

</style>